package java8;

import com.xml.Person;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 方法的引用
 */
public class MethodDemo {

    public static void main(String[] args) {
//        staticMethod();
//        new MethodDemo().exampleMethod();
//        new MethodDemo().objectMethod();
        new MethodDemo().constrctorMethod();
    }

    /**
     * 静态方法引用
     */
    public static void staticMethod() {
        Supplier<String> supplier = () -> MethodDemo.put();
        Supplier<String> supplier2 = MethodDemo::put;
        System.out.println(supplier.get());
        System.out.println(supplier2.get());

        Consumer<Integer> consumer = (size) -> MethodDemo.con(size);
        Consumer<Integer> consumer2 = MethodDemo::con;
        consumer.accept(100);
        consumer2.accept(100);

        Function<String, String> f1 = str -> MethodDemo.toUpperCase(str);
        Function<String, String> f2 = MethodDemo::toUpperCase;
        f1.apply("12");
        f2.apply("12");
    }


    /**
     * 实例方法引用
     */
    public void exampleMethod() {
        Supplier<String> supplier = () -> new MethodDemo().puts();
        Supplier<String> supplier2 = new MethodDemo()::puts;

        MethodDemo methodDemo = new MethodDemo();
        Supplier<String> supplier3 = methodDemo::puts;
        Supplier<String> supplier4 = this::puts;
        Supplier<String> supplier5 = super::toString;
    }


    /**
     * 对象方法的引用
     */
    public void objectMethod() {
        Consumer<TOO> consumer = (TOO too) -> new TOO().foo();
        Consumer<TOO> consumer1 = TOO::foo;
        consumer.accept(new TOO());
        consumer1.accept(new TOO());


        BiConsumer<TOO, String> consumer2 = (TOO too, String str) -> {
            new TOO().fo(str);
        };
        consumer2.accept(new TOO(), "11");
        BiConsumer<TOO, String> consumer3 = TOO::fo;
        consumer3.accept(new TOO(), "22");

    }


    /**
     * 构造方法的引用
     */
    public void constrctorMethod() {
        Supplier<Penson> s1 = () -> new Penson();
        Supplier<Penson> s2 = Penson::new;
        s1.get();
        s2.get();

        Supplier<String> s3 = String::new;
        Supplier<Thread> s4 = Thread::new;

        Consumer<String> c1 = (size) -> new Penson(size);
        Consumer<String> c2 = Penson :: new;


    }


    public static String put() {
        return "hello";
    }

    public String puts() {
        return "hello";
    }

    public static void con(Integer size) {
        System.out.println(size);
    }

    public static String toUpperCase(String str) {
        System.out.println(str);
        return str;
    }

}

class TOO {
    public void foo() {
        System.out.println("invoke");
    }

    public void fo(String str) {
        System.out.println("invokeStr" + str);
    }
}


class Penson {

    public Penson() {
        System.out.println("new Person()");
    }

    public Penson(String size) {
        System.out.println("new Person()" + size);
    }
}


